const Base = require('./base.js');

module.exports = class extends Base {
  /**
   * 获取用户的挂机玩法信息
   * @return {Promise} []
   */
  async getUserInfoAction() {
    // const wallet_address = this.post('wallet_address');
    const id = this.getLoginUserId();
    const user = await this.model('gameuser')
      .where({ id: id })
      .find();

    return this.success({
      cue_time: parseInt(Date.now() / 1000),
      userInfo: user
    });
  }

  /**
  * 加时间
  *  @return {Promise} []
  */
  async addTimeAction() {
    // const wallet_address = this.post('wallet_address');
    const id = this.getLoginUserId();
    let automatic_bonus_time = this.post('automatic_bonus_time');

    const user = await this.model('gameuser')
      .where({ id: id })
      .find();

    const c_time = parseInt(Date.now() / 1000);
    let newBegin_automatic_bonus_time = c_time;
    let newAutomatic_bonus_time = user.automatic_bonus_time;
    let newGems = user.automatic_bonus_time;
    let newDay_automatic_bonus_time = user.day_automatic_bonus_time;

    //用户当前自动时间为0，则更新当天开始获取自动时间
    if (user.automatic_bonus_time < 240 && newDay_automatic_bonus_time < 240) {
      await this.model('gameuser')
        .where({ id: id })
        .update({
          begin_automatic_bonus_time: newBegin_automatic_bonus_time
        });
    };

    if (newDay_automatic_bonus_time < 43200) {
      automatic_bonus_time = newDay_automatic_bonus_time + automatic_bonus_time <= 43200
        ? automatic_bonus_time : 43200 - newDay_automatic_bonus_time;

      newDay_automatic_bonus_time += automatic_bonus_time;

      newAutomatic_bonus_time = user.automatic_bonus_time + automatic_bonus_time;
      newGems = user.gems + automatic_bonus_time / 3600;

    } else {
      return this.fail(506, '当日获取自动时间已经超过12个小时，请明日再来获取');
    };

    await this.model('gameuser')
      .where({ id: id })
      .update({
        automatic_bonus_time: newAutomatic_bonus_time,
        gems: newGems,
        day_automatic_bonus_time: newDay_automatic_bonus_time
      });

    const user2 = await this.model('gameuser')
      .where({ id: id })
      .find();

    return this.success({
      hashrate: user2.hashrate,
      gems: user2.gems,
      automatic_bonus_time: user2.automatic_bonus_time,
      day_automatic_bonus_time: user2.day_automatic_bonus_time
    });
  };

  // /**
  // * 更新时间与算力
  // *  @return {Promise} []
  // */
  // async updataTimeAction() {
  //   const wallet_address = this.post('wallet_address');

  //   const user = await this.model('gameuser')
  //     .where({ wallet_address: wallet_address })
  //     .find();

  //   const c_time = parseInt(Date.now() / 1000);
  //   const cur_last_diff_time = c_time - user.last_login_time;

  //   let newAutomatic_Bonus_Time = user.automatic_bonus_time;
  //   let newhashrate = user.hashrate;

  //   if (user.day_activate > 0 && user.automatic_bonus_time > 1 && user.automatic_bonus_time > cur_last_diff_time) {
  //     newAutomatic_Bonus_Time = user.automatic_bonus_time - cur_last_diff_time;
  //     newhashrate += cur_last_diff_time;
  //   } else if (user.automatic_bonus_time < cur_last_diff_time) {
  //     newAutomatic_Bonus_Time = 0;
  //     newhashrate += user.automatic_bonus_time;
  //   }

  //   await this.model('gameuser')
  //     .where({ wallet_address: wallet_address })
  //     .update({
  //       automatic_bonus_time: newAutomatic_Bonus_Time,
  //       hashrate: newhashrate,
  //       last_login_time: c_time
  //     });

  //   const user2 = await this.model('gameuser')
  //     .where({ wallet_address: wallet_address })
  //     .find();

  //   return this.success({
  //     hashrate: user2.hashrate,
  //     gems: user2.gems,
  //     automatic_bonus_time: user2.automatic_bonus_time,
  //     day_automatic_bonus_time: user2.day_automatic_bonus_time
  //   });
  // };

  /**
 * 获取算力
 *  @return {Promise} []
 */
  async updataTimeAction() {
    // const wallet_address = this.post('wallet_address');

    const id = this.getLoginUserId();
    const user = await this.model('gameuser')
      .where({ id: id })
      .find();

    return this.success({
      hashrate: user.hashrate,
      gems: user.gems,
      automatic_bonus_time: user.automatic_bonus_time,
      day_automatic_bonus_time: user.day_automatic_bonus_time,
      underling_hashrate: user.underling_hashrate
    });
  };

  //   /**
  // * 获取用户下属算力
  // *  @return {Promise} []
  // */
  //   async updataUnderlingCPAction() {
  //     const wallet_address = this.post('wallet_address');

  //     const user = await this.model('gameuser')
  //       .where({ wallet_address: wallet_address })
  //       .find();

  //     let newUnderling_hashrate = 0;

  //     const unders = await this.model('gameuser')
  //       .where({
  //         lead_InvitationCode: user.m_InvitationCode,
  //         day_activate: 1
  //       })
  //       .select();

  //     if (unders && unders.length > 0) {
  //       unders.map((v) => {
  //         newUnderling_hashrate += v.hashrate * 0.3 * 10 / 10;
  //       });
  //     }

  //     if (newUnderling_hashrate > 0) {
  //       await this.model('gameuser')
  //         .where({ wallet_address: wallet_address })
  //         .update({
  //           underling_hashrate: newUnderling_hashrate
  //         });

  //       const user2 = await this.model('gameuser')
  //         .where({ wallet_address: wallet_address })
  //         .find();

  //       return this.success({
  //         underling_hashrate: user2.underling_hashrate
  //       });
  //     } else {
  //       return this.success({
  //         underling_hashrate: 0
  //       });
  //     }
  //   };

  /**
 * 获取用户下属算力
 *  @return {Promise} []
 */
  async updataUnderlingCPAction() {
    // const wallet_address = this.post('wallet_address');

    const id = this.getLoginUserId();
    const user = await this.model('gameuser')
      .where({ id: id })
      .find();

    return this.success({
      underling_hashrate: user.underling_hashrate
    });
  };

  /**
* 每天对所有当天激活过的用户清零——算力、自动时间、当天自动时间、开始自动时间、当天激活状态5项
*  @return {Promise} []
*/
  async resetDay5DataAction() {
    if (!this.isCli) return this.fail(1000, 'deny');

    const unders = await this.model('gameuser')
      .where({
        day_activate: 1
      })
      .select();

    if (unders && unders.length > 0) {
      unders.map(async (v) => {
        await this.model('gameuser')
          .where({ wallet_address: v.wallet_address })
          .update({
            day_automatic_bonus_time: 0,
            begin_automatic_bonus_time: 0,
            automatic_bonus_time: 0,
            day_activate: 0,
            underling_hashrate: 0,
            hashrate: 0
          });
      });
    }
  };

  /**
* 每5秒对所有当天激活过的用户计算——自己算力、自动时间
*  @return {Promise} []
*/
  async resetSecondData2Action() {
    if (!this.isCli) return this.fail(1001, 'deny');

    const unders = await this.model('gameuser')
      .where({
        day_activate: 1,
        automatic_bonus_time: ['>=', 5]
      })
      .select();

    if (unders && unders.length > 0) {
      unders.map(async (v) => {
        await this.model('gameuser')
          .where({ wallet_address: v.wallet_address })
          .update({
            hashrate: v.hashrate + 5,
            automatic_bonus_time: v.automatic_bonus_time - 5
          });
      });
    }
  };

  /**
  * 每1分钟更新所有当天激活过的用户计算——自己下属的算力
  *  @return {Promise} []
  */
  async resetUnderlingCPAction() {
    if (!this.isCli) return this.fail(1002, 'deny');

    const unders = await this.model('gameuser')
      .where({
        day_activate: 1
      })
      .select();

    if (unders && unders.length > 0) {
      unders.map(async (v) => {
        let newUnderling_hashrate = 0;
        const underCPs = await this.model('gameuser')
          .where({
            lead_InvitationCode: v.m_InvitationCode,
            day_activate: 1
          })
          .select();

        if (underCPs && underCPs.length > 0) {
          underCPs.map((vCP) => {
            newUnderling_hashrate += vCP.hashrate * 0.3 * 10 / 10;
          });
        }
        if (newUnderling_hashrate > 0) {
          await this.model('gameuser')
            .where({ wallet_address: v.wallet_address })
            .update({
              underling_hashrate: newUnderling_hashrate
            });
        }
      });
    }
  };

  /**
  *
  *  @return {Promise} []
  */
  async loginUpdataTimeAction() {
    // const wallet_address = this.post('wallet_address');

    const id = this.getLoginUserId();
    const user = await this.model('gameuser')
      .where({ id: id })
      .find();

    const c_time = parseInt(Date.now() / 1000);
    const cur_last_diff_time = c_time - user.last_login_time;
    var OfflineReward = 0;
    var new_automatic_bonus_time = 0;
    var new_hashrate = user.hashrate;
    var new_day_automatic_bonus_time = user.day_automatic_bonus_time;
    var new_begin_automatic_bonus_time = user.begin_automatic_bonus_time;

    //隔了一天，自动时间清零情况下的离线奖励计算
    if (cur_last_diff_time > 86400 || new Date().getDate() != new Date(user.last_login_time * 1000).getDate() || new Date().getMonth() != new Date(user.last_login_time * 1000).getMonth()) {
      OfflineReward = user.automatic_bonus_time;
      new_automatic_bonus_time = 0;
      new_day_automatic_bonus_time = 0;
      new_hashrate = 0;
      new_begin_automatic_bonus_time = 0;
    }
    else if (new Date().getDate() == new Date(user.last_login_time * 1000).getDate() && user.automatic_bonus_time > 0) {
      //当天，自动奖励时间有剩余
      if (user.automatic_bonus_time > cur_last_diff_time) {
        OfflineReward = cur_last_diff_time;
        new_automatic_bonus_time = user.automatic_bonus_time - cur_last_diff_time;
        let t_hashrate = user.hashrate + OfflineReward;
        new_hashrate = t_hashrate > 43200 ? 43200 : t_hashrate;
      }
      //当天，自动奖励时间为0
      else {
        OfflineReward = user.automatic_bonus_time;
        new_automatic_bonus_time = 0;
        let t_hashrate = user.hashrate + OfflineReward;
        new_hashrate = t_hashrate > 43200 ? 43200 : t_hashrate;
      }
    };

    await this.model('gameuser')
      .where({ id: id })
      .update({
        automatic_bonus_time: new_automatic_bonus_time,
        hashrate: new_hashrate,
        last_login_time: c_time,
        day_activate: 1,
        begin_automatic_bonus_time: 0,
        day_automatic_bonus_time: new_day_automatic_bonus_time
      });

    const user2 = await this.model('gameuser')
      .where({ id: id })
      .find();

    return this.success({
      m_InvitationCode: user2.m_InvitationCode,
      hashrate: user2.hashrate,
      gems: user2.gems,
      automatic_bonus_time: user2.automatic_bonus_time,
      underling_limit: user2.underling_limit,
      day_automatic_bonus_time: user2.day_automatic_bonus_time
    });
  };
}

